package org.jcut.service.impl;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradePagePayRequest;

import org.jcut.bean.Commodity;
import org.jcut.bean.Orderform;
import org.jcut.config.AlipayConfig;

import org.jcut.dao.IOrderform;
import org.jcut.service.ICommodityService;
import org.jcut.service.PayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;
import java.util.UUID;

@Transactional
@Service
public class PayServiceImpl implements PayService {
	double myPri = 0.0;
	String[] myprice = null;
	String[] myNum = null;
	String[] myCid = new String[0];
	@Autowired
	private IOrderform dao;
	@Autowired
	private ICommodityService cdao;

	@Override
	public String PayMoney(Map<String, String> PayMap, Orderform orderform) throws Exception {

		if (orderform.getMyPrice().indexOf(",") == -1) {

			orderform.setId(UUID.randomUUID().toString());

			orderform.setoPrice(Double.parseDouble(orderform.getMyPrice()));
			orderform.setcNum(Integer.parseInt(orderform.getMyNum()));
			orderform.setTime(new Date());

			Commodity commodity = new Commodity();
			commodity.setId(orderform.getcId());
			commodity.setSales(Integer.parseInt(orderform.getMyNum()));
			int i = cdao.buyCommodity(commodity);
			System.out.println("==" + i);
			dao.insert(orderform);
			PayMap.put("total_amount", orderform.getMyPrice());
		} else if (orderform.getMyPrice().indexOf(",") > 0) {

			myprice = orderform.getMyPrice().split(",");
			myNum = orderform.getMyNum().split(",");
			myCid = orderform.getcId().split(",");

			for (String sp : myprice) {
				myPri = Double.parseDouble(sp) + myPri;
			}
			PayMap.put("total_amount", String.valueOf(myPri));
		}

		new Thread() {

			@Override
			public void run() {
				
					if (myprice != null) {
						for (int i = 0; i < myprice.length; i++) {

							orderform.setId(UUID.randomUUID().toString());
							orderform.setcId(myCid[i]);
							orderform.setoPrice(Double.parseDouble(myprice[i]));
							orderform.setcNum(Integer.parseInt(myNum[i]));
							orderform.setTime(new Date());

							Commodity commodity = new Commodity();
							commodity.setId(myCid[i]);
							commodity.setSales(Integer.parseInt(myNum[i]));
							cdao.buyCommodity(commodity);
							dao.insert(orderform);
						}
					
				}
			}

		}.start();

		AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.APPID,
				AlipayConfig.RSA_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY,
				AlipayConfig.SIGNTYPE);
		AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
		alipayRequest.setReturnUrl(AlipayConfig.return_url);
		alipayRequest.setNotifyUrl(AlipayConfig.notify_url);
		AlipayTradeWapPayModel alipayTradeWapPayModel = new AlipayTradeWapPayModel();
		alipayTradeWapPayModel.setOutTradeNo(PayMap.get("out_trade_no"));
		alipayTradeWapPayModel.setProductCode("FAST_INSTANT_TRADE_PAY");
		alipayTradeWapPayModel.setTotalAmount(PayMap.get("total_amount"));
		alipayTradeWapPayModel.setSubject("测试使用");// 标题
		alipayTradeWapPayModel.setBody(PayMap.get("body"));
		alipayRequest.setBizModel(alipayTradeWapPayModel);
		String form = alipayClient.pageExecute(alipayRequest).getBody();

		return form;

	}
}
